<template>
    <div class="page">
      <div class="page-top">
        <!-- 面包屑 -->
        <Hreadcrumb :hreadcrumbAry="hreadcrumbAry" />
      </div>
      <div class="page-content">
        <a-form-model layout="inline" style="margin-bottom: 20px" :form="form">
          <a-form-model-item label="订单类型">
            <a-select default-value="全部" style="width: 100px" v-model="form.databaseName">
              <a-select-option value="SaleDeliveryUpdateHistory">
                销售出库
              </a-select-option>
              <a-select-option value="SaleBackUpdateHistory">
                销售退货
              </a-select-option>
              <a-select-option value="PurchaseOrderUpdateHistory">
                采购订单
              </a-select-option>
              <a-select-option value="PurchaseBackUpdateHistory">
                采购退货
              </a-select-option>
              <a-select-option value="TransVoucherUpdateHistory">
                调拨单
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="订单状态">
            <a-select default-value="全部" style="width: 300px" v-model="form.hasDeal">
              <a-select-option value="-1"> 全部 </a-select-option>
              <a-select-option value="0"> 未处理 </a-select-option>
              <a-select-option value="1"> 处理失败 </a-select-option>
              <a-select-option value="2"> 处理成功 </a-select-option>
              <a-select-option value="3"> 采购入库回写成功 </a-select-option>
              <a-select-option value="4"> 反审核(取消)成功 </a-select-option>
              <a-select-option value="5"> 手动取消成功 </a-select-option>
              <a-select-option value="6">
                已关闭(非京东,自动关闭)
              </a-select-option>
              <a-select-option value="7">
                已关闭(自提单,地址非京东仓库,自动关闭)
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="创建时间">
            <a-range-picker show-time format="YYYY-MM-DD HH:mm:ss" v-model="timeValue" @change="bindTime" />
          </a-form-model-item>
          <a-form-model-item label="订单编号">
            <a-input v-model="form.code"></a-input>
          </a-form-model-item>
          <a-form-model-item label="京东单号">
            <a-input v-model="form.jdCode"></a-input>
          </a-form-model-item>
          <a-form-model-item label="金蝶数据中心" in>
            <!-- <a-input style="width:300px" v-model="detail.jdDept"></a-input> -->
            <a-select style="width: 200px" v-model="form.dataSource" @change="handleDataSpirce">
              <a-select-option v-for="item in this.centerData" :key="item.dataCenterId">{{ item.dataCenterName
              }}</a-select-option>
              <!-- <a-select-option value="998">测试中心</a-select-option> -->
              <!-- <a-select-option value="guanyi">管易</a-select-option> -->
            </a-select>
          </a-form-model-item>
          <a-form-model-item>
            <a-button type="primary" html-type="submit" @click="onSubmit"> 提交 </a-button>
            <a-button style="margin-left: 15px" @click="bindReset">
              重置
            </a-button>
          </a-form-model-item>
        </a-form-model>
        <a-table :columns="columns" :data-source="data" :loading="loading" :pagination="pagination"
          @change="handleTableChange">
          <span slot="orderType">{{
            this.databaseName == "SaleDeliveryUpdateHistory"
            ? "销售出库"
            : this.databaseName == "SaleBackUpdateHistory"
              ? "销售退货"
              : this.databaseName == "PurchaseOrderUpdateHistory"
                ? "采购订单"
                : this.databaseName == "PurchaseBackUpdateHistory"
                  ? "采购退货"
                  : this.databaseName == "TransVoucherUpdateHistory"
                    ? "调拨单"
                    : ""
          }}</span>
          <span slot="OperateType" slot-scope="record">{{
            record == "C" ? "审核" : record == "D" ? "反审核" : ""
          }}</span>
          <span slot="HasDeal" slot-scope="record">{{
            record == "0"
            ? "未处理"
            : record == "1"
              ? "处理失败"
              : record == "2"
                ? "处理成功"
                : record == "3"
                  ? "采购入库回写成功"
                  : record == "4"
                    ? "反审核（取消）成功"
                    : record == "5"
                      ? "手动取消成功"
                      : record == "6"
                        ? "已关闭（非京东单，自动关闭）"
                        : record == "7"
                          ? "已关闭（自提单，地址非京东仓库，自动关闭）"
                          : ""
          }}</span>
          <span slot="ScsHasDeal" slot-scope="record">{{
            record == "0"
            ? "未处理"
            : record == "1"
              ? "处理失败"
              : record == "2"
                ? "处理成功"
                : "关闭"
          }}</span>
          <span slot="options" slot-scope="text, record">
            <a-button type="primary" style="margin-right: 15px"
              :hidden="record.HasDeal != '5' && record.HasDeal != '1' ? true : false"
              @click="changeKDlogList(record, '0')">重传</a-button>
            <a-button type="primary" :hidden="record.HasDeal != '0' ? true : false"
              @click="changeKDlogList(record, '5')">拦截</a-button>
          </span>
          <span slot="SCSoptions" slot-scope="text, record">
            <a-button type="primary" style="margin-right: 15px"
              :hidden="record.ScsHasDeal != '5' && record.ScsHasDeal != '1' ? true : false"
              @click="changeKDSCSlogList(record, '0')">重传</a-button>
            <!-- <a-button
              type="primary"
              :hidden="record.ScsHasDeal != '0' ? true : false"
              @click="changeKDSCSlogList(record, '5')"
              >拦截</a-button
            > -->
          </span>
        </a-table>
      </div>
    </div>
  </template>
  <script>
  import Hreadcrumb from "@/components/Hreadcrumb";
  import { getKDlogList, changeKDlogList, changeKDSCSlogList, getAllDataCenter } from "@/request/api";
  export default {
    data() {
      return {
        loading: false,
        hreadcrumbAry: [
          { name: "首页", path: "/", query: {} },
          { name: "日志管理", path: "", query: {} },
          { name: "金蝶日志", path: "/KDlog", query: {} },
        ],
        columns: [
          {
            title: "单据类型",
            dataIndex: "",
            key: "",
            slots: { title: "单据类型" },
            scopedSlots: { customRender: "orderType" },
            width: 90
          },
          {
            title: "ERP单号",
            dataIndex: "Code",
            key: "Code",
          },
          {
            title: "操作状态",
            dataIndex: "OperateType",
            key: "OperateType",
            slots: { title: "操作状态" },
            scopedSlots: { customRender: "OperateType" },
            width: 90
          },
          {
            title: "操作时间",
            dataIndex: "CreateTime",
            key: "CreateTime",
            width: 120,
          },
          {
            title: "运行时间",
            dataIndex: "UpdateTime",
            key: "UpdateTime",
            width: 120,
          },
          {
            title: "单号",
            dataIndex: "JDNo",
            key: "JDNo",
          },
          {
            title: "状态",
            dataIndex: "HasDeal",
            key: "HasDeal",
            slots: { title: "状态" },
            scopedSlots: { customRender: "HasDeal" },
            width: 160,
          },
          {
            title: "操作",
            dataIndex: "options",
            key: "options",
            slots: { title: "操作" },
            scopedSlots: { customRender: "options" },
            width: "100px",
          },
          {
            title: "推送平台状态",
            dataIndex: "ScsHasDeal",
            key: "ScsHasDeal",
            slots: { title: "推送平台状态" },
            scopedSlots: { customRender: "ScsHasDeal" },
            width: 120
          },
          {
            title: "推送平台操作",
            dataIndex: "SCSoptions",
            key: "SCSoptions",
            slots: { title: "操作" },
            scopedSlots: { customRender: "SCSoptions" },
            width: "120px",
          },
        ],
        pagination: { current: 1, size: 10 },
        data: [],
        form: {
          databaseName: "SaleDeliveryUpdateHistory",
          hasDeal: "-1",
          startTime: "",
          endTime: "",
          jdCode: "",
          code: "",
          dataSource: ""
        },
        databaseName: '',
        timeValue: undefined,
        centerData: []
      };
    },
    components: {
      Hreadcrumb,
    },
    created() {
      this.getAllDataCenter().then(res=>{
        this.getKDlogList(1,10)
      })
    },
    methods: {
      async getKDlogList(p, ps) {
        this.loading = true;
        let { startTime, endTime, databaseName, hasDeal, jdCode, code, dataSource } =
          this.form;
        let params =
          "p=" +
          (p - 1) +
          "&ps=" +
          ps +
          "&databaseName=" +
          databaseName +
          "&code=" +
          code +
          "&jdCode=" +
          jdCode +
          "&hasDeal=" +
          hasDeal +
          "&startTime=" +
          startTime +
          "&endTime=" +
          endTime +
          "&systemType=4&dataSource=" +
          dataSource;
        let { data } = await getKDlogList(params);
        console.log(data);
        if (data.success) {
          this.loading = false;
          let pagination = { ...this.pagination };
          pagination.total = data.content.total;
          this.pagination = pagination;
          this.data = data.content.list;
          this.databaseName = this.form.databaseName
        } else {
          this.loading = false;
          this.data = [];
        }
      },
  
      bindTime(date, dateString) {
        console.log(date, dateString);
        this.form.startTime = dateString[0];
        this.form.endTime = dateString[1];
      },
      bindReset() {
        this.form = {
          databaseName: "SaleDeliveryUpdateHistory",
          hasDeal: "-1",
          startTime: "",
          endTime: "",
          jdCode: "",
          code: "",
          dataSource: ""
        };
        this.timeValue = undefined;
        this.getKDlogList(1, 10);
      },
      onSubmit(e) {
        e.preventDefault();
        console.log(this.form);
        this.getKDlogList(1, 10);
      },
      handleTableChange(pagination) {
        console.log(pagination);
        const pager = { ...this.pagination };
        pager.current = pagination.current;
        this.pagination = pager;
        this.getKDlogList(pager.current, pager.size);
      },
      handleDataSpirce(e) {
        this.form.dataSource = e
      },
      async changeKDlogList(record, hasDeal) {
        let param =
          "databaseName=" +
          this.form.databaseName +
          "&id=" +
          record.ID +
          "&hasDeal=" +
          hasDeal +
          "&updateBy=" +
          JSON.parse(localStorage.getItem("userInfo")).userName +
          "&dataSource=" +
          this.form.dataSource;
        let { data } = await changeKDlogList(param);
        if (data.success) {
          // this.getKDlogList(this.pagination.current,10);
          this.$message.success("操作成功");
        } else {
          this.$message.error(data.content);
        }
      },
      async changeKDSCSlogList(record, ScsHasDeal) {
        let param =
          "databaseName=" +
          this.form.databaseName +
          "&id=" +
          record.ID +
          "&scsHasDeal=" +
          ScsHasDeal +
          "&updateBy=" +
          JSON.parse(localStorage.getItem("userInfo")).userName +
          "&dataSource=" +
          this.form.dataSource;
        let { data } = await changeKDSCSlogList(param);
        if (data.success) {
          // this.getKDlogList(this.pagination.current,10);
          this.$message.success("操作成功");
        } else {
          this.$message.error(data.content);
        }
      },
      async getAllDataCenter() {
        let { data } = await getAllDataCenter()
        console.log(data)
        if (data.success) {
          this.centerData = data.content
          this.form.dataSource = data.content[0].dataCenterId
          return data.content
        }
      },
    },
  };
  </script>
  <style>
  .page-content {
    background-color: #fff;
    padding: 25px;
  }
  </style>